//
//  KZListen&SpeakVocabularyImporveVC.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/22.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZListenSpeakVocabularyImporveVC.h"

// vc
#import "KZVocabularyImproveMakeTestVC.h"
#import "KZUserCenterVC.h"
#import "KZVocabularyImproveMakeTestVC_version2.h"

// view
#import "KZRandomDistributionView.h"        // 随机分布视图
#import "KZShadowButton.h"                  // 阴影按钮
#import "KZRandomSphereView.h"
#import "KZAlertView.h"                     // 弹框

// api
#import "KZVocabularyImproveNetApi.h"

@interface KZListenSpeakVocabularyImporveVC ()



@property (nonatomic, strong) KZRandomSphereView *disView;
// 掌握词汇
@property (nonatomic, strong) UILabel *masterLabel;

@property (nonatomic, strong) KZVocabularyPackageModel *packageModel;

@end

@implementation KZListenSpeakVocabularyImporveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.layer.contentsGravity = kCAGravityResizeAspectFill;
    self.view.layer.contents = (__bridge id)[UIImage imageNamed:@"kz-听说词汇底图大背景"].CGImage;

    [self setupNaviUI];
    [self setupUI];
    
    [self.navigationController zk_registerBackInteractive:nil gestureConfig:nil];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:true stateBarLightContent:true];
    // 获取词汇包信息
    [self getListenSayVocabularyPackageInfo];
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [_disView removeAnimation];
}

#pragma mark - 创建视图
- (void)setupNaviUI {
    
//    self.title = @"听说词汇强化学习";
//    [self addLeftBackBarButtonItem];
    
    NSString *text = @"听说词汇强化学习";
    
    UIControl *backView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    [backView addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//    backImageView.image = [UIImage imageNamed:@"kz-关闭"];
    backImageView.image = [UIImage imageNamed:@"LS-nav_back"];
    [backView addSubview:backImageView];
    [backImageView sizeToFit];
    backImageView.centerY = backView.height / 2;
    
//    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(backImageView.right+5, 10, 20, 20)];
//    iconView.image = [UIImage imageNamed:@"kz-左上角听说词汇强化学习图标"];
//    [backView addSubview:iconView];
//    [iconView sizeToFit];
//    iconView.centerY = backView.height / 2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(backImageView.right+5, 0, [text getSizeOfBlodString:20].width, 40)];
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = text;
    label.font = B_FONT(20);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [backView addSubview:label];
    
    backView.width = label.right;
    
    UIButton *noteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    noteButton.frame = CGRectMake(0, 0, 17, 17);
    [noteButton setImage:[UIImage imageNamed:@"kz-问号"] forState:UIControlStateNormal];
    [noteButton addTarget:self action:@selector(note) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:noteButton];
}

- (void)setupUI {
    
    _disView = [KZRandomSphereView new];
    [self.view addSubview:_disView];
    [_disView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        } else {
            make.top.equalTo(self.view).offset(64);
        }
        make.width.equalTo(self.view).multipliedBy(0.9);
        make.height.equalTo(_disView.mas_width).multipliedBy(0.8);
    }];
    
    // 按钮
    KZShadowButton *beginButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor;
        [button setTitle:@"开始学习" forState:UIControlStateNormal];
    }];
    [beginButton addTarget:self action:@selector(begin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:beginButton];
    [beginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.height.mas_equalTo(W(40));
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-W(30));
        } else {
            make.bottom.mas_equalTo(-W(30));
        }
        
    }];
    
   
//    // 标注
//    UILabel *noteLabel = [[UILabel alloc] init];
//    noteLabel.text = @"基于中考目标，大数据分析出需要加强的听说词汇进行专业的词汇训练";
//    noteLabel.font = FONT(12);
//    noteLabel.numberOfLines = 0;
//    noteLabel.textAlignment = NSTextAlignmentCenter;
//    noteLabel.textColor = [UIColor lightGrayColor];
//    [self.view addSubview:noteLabel];
//    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@10);
//        make.right.equalTo(@-10);
//        make.bottom.equalTo(beginButton.mas_top).offset(-W(20));
//    }];
    // 标注
    _masterLabel = [[UILabel alloc] init];
    _masterLabel.font = FONT(H(14));
    _masterLabel.textAlignment = NSTextAlignmentCenter;
    _masterLabel.textColor = [UIColor grayColor];
    [self.view addSubview:_masterLabel];
    [_masterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.bottom.equalTo(beginButton.mas_top).offset(-H(40));
    }];
//    // 标注
//    UILabel *note2Label = [[UILabel alloc] init];
//    note2Label.text = @"当前词汇掌握分析";
//    note2Label.font = FONT(16);
//    note2Label.textAlignment = NSTextAlignmentCenter;
//    note2Label.textColor = [UIColor blackColor];
//    [self.view addSubview:note2Label];
//    [note2Label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(_masterLabel);
//        make.bottom.equalTo(_masterLabel.mas_top).offset(-5);
//    }];


}

#pragma mark - Net
- (void)getListenSayVocabularyPackageInfo {
    
    [MBProgressHUD showLoadind:@"正在查询待复习听说词汇，请稍候..."];
    
    KZVocabularyImproveNetApi *api = [KZVocabularyImproveNetApi new];
    api.requestParam = @{@"stageNo":KGetUserDefaults(@"TestLayer"),
                         @"schoolId":DR_UserInfo.SchoolID,
                         @"stuId":DR_UserInfo.UserID,};
    [api getListenSayVocabularyPackageInfo:^(KZVocabularyPackageModel *model) {
        
        _packageModel = model;
        
        [MBProgressHUD hideHud];
        
        if (_packageModel.currentPackage.Detail.count > 20) {
            _disView.vocabularys = [_packageModel.currentPackage.Detail subarrayWithRange:NSMakeRange(0, 20)];
        } else {
            _disView.vocabularys = _packageModel.currentPackage.Detail;
        }
        
        NSString *text = [NSString stringWithFormat:@"掌握较好%@个，较弱%@个", model.BetterMasterVocabularyNum, model.PoorMasterVocabularyNum];
        NSString *better = [NSString stringWithFormat:@"%@", model.BetterMasterVocabularyNum];
        NSString *poor = [NSString stringWithFormat:@"%@", model.PoorMasterVocabularyNum];
        NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
        [attrText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:[text rangeOfString:better]];
        [attrText addAttribute:NSFontAttributeName value:FONT(H(18)) range:[text rangeOfString:better]];
        [attrText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:[text rangeOfString:poor]];
        [attrText addAttribute:NSFontAttributeName value:FONT(H(18)) range:[text rangeOfString:poor]];

        _masterLabel.attributedText = attrText.copy;

    } failuer:^(NSString *error) {
        
        [MBProgressHUD showFailureWithText:error];
    }];
}

#pragma mark - 事件
- (void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}
// 问号备注
- (void)note {
    
    // 弹框提示
    NSMutableAttributedString *subtitle = [[NSMutableAttributedString alloc] initWithString:@"基于高考目标，大数据分析出需要加强的听说词汇进行专业的词汇训练"];
    
    KZAlertView *alert = [KZAlertView new];
    [alert showWithTitle:@"温馨提示" subtitle:subtitle submitTypes:@[@1] submitButtonsConfig:^(NSArray<UIButton *> *buttons) {
        
        [buttons.firstObject setTitle:@"好的" forState:UIControlStateNormal];
    } click:^(NSInteger index) {
        
    } destroyAtClickIndex:@[@0]];
}

- (void)begin {
    if (!_packageModel) {
        return;
    }
    if (_packageModel.currentPackage.Detail.count == 0) {
        [MBProgressHUD showFailureWithText:@"没有词汇包需要训练了！"];
        return;
    }
    
    KZVocabularyImproveMakeTestVC_version2 *vc = [KZVocabularyImproveMakeTestVC_version2 new];
    vc.type = KZVocabularyTypeLS;
    vc.packageModel = _packageModel;
    [self.navigationController pushViewController:vc animated:true];
}



@end
